<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="keys" content="">
    <meta name="author" content="">
	<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
	<link rel="stylesheet" href="css/font-awesome.min.css">
	<link rel="stylesheet" href="css/login.css">
	<link rel="stylesheet" href="css/style1.css">
  
	<style>

	</style>
  </head>
  <body>
    <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
      <div class="container">
        <div class="navbar-header">
          <div><a class="navbar-brand" href="index.html" style="font-size:32px;">尚筹网-创意产品众筹平台</a></div>
        </div>
      </div>
    </nav>

    <div class="container">

      <form class="form-signin" role="form">
        <h2 class="form-signin-heading"><i class="glyphicon glyphicon-log-in"></i> 用户登录</h2>
		  <div class="form-group has-success has-feedback">
			<input type="text" class="form-control" id="loginacct" placeholder="请输入登录账号" autofocus>
			<span class="glyphicon glyphicon-user form-control-feedback"></span>
		  </div>
		  <div class="form-group has-success has-feedback">
			<input type="password" class="form-control" id="password" placeholder="请输入登录密码" style="margin-top:10px;">
			<span class="glyphicon glyphicon-lock form-control-feedback"></span>
		  </div>
		 
		  <div>
	  		 <label>验证码</label>
              <input type="text" placeholder="请输入验证码..." name="code" id="code">
             <a href="javascript:change();"> <img src="getValidCode" id="pic" style="width:80px" height="40px"></a>
		  </div>
		 <div id="slideBar"></div>
        <div class="checkbox">
         
          <br>
          <label>
           <a href="member_findPwd"> 忘记密码</a>
          </label>
          <label style="float:right">
            <a href="member_toreg">我要注册</a>
          </label>
        </div>
       
      </form>
    </div>
     <div id="page" class="page_div"></div>
    <script src="jquery/jquery-2.1.1.min.js"></script>
    <script src="bootstrap/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="layer/layer.js"></script>
    <script type="text/javascript" src="jquery/style.js"></script>
    <script>
    var dataList = ["0","1"];
    var options = {
        dataList: dataList,
        success:function(){  
           dologin();
        },
        fail: function(){
             
        }
    };
    SliderBar("slideBar", options);

    function dologin() {
     	var loginacct=$("#loginacct").val();
     	var password=$("#password").val();
     	var code=$("#code").val();
     	if(loginacct=="" || password==""){
     		layer.msg("请输入用户名和密码", {
	    	    time: -1, //20s后自动关闭
	    	    icon:5,
	    	   /*  shift:6, */
	    	    btn: ['知道了','明白了']
	    	 });
     		return;
     	}
     	if(code.length!=4){
     		layer.msg("请输入4位验证码", {
	    	    time: -1, //20s后自动关闭
	    	    icon:5,
	    	   /*  shift:6, */
	    	    btn: ['知道了','明白了']
	    	 });
     		return;
     	}
     	$.post(
     		"member_login",
     		{"loginacct":loginacct,"password":password,"code":code},
     		function(data){
     			if(data.code==0){
     				layer.msg(data.message, {
     		    	    time: -1, //20s后自动关闭
     		    	    icon:5,
     		    	   /*  shift:6, */
     		    	    btn: ['知道了','明白了']
     		    	 });
     				return;
     			}
     			window.location.href="index";
     		}
     	);
    }
    
    function change() {
		document.getElementById("pic").src = "getValidCode?time=" + new Date().getTime();
	}
    
    (function (window) {
    	  const l = 42, // 滑块边长
    	    r = 10, // 滑块半径
    	    w = 310, // canvas宽度
    	    h = 155, // canvas高度
    	    PI = Math.PI
    	  const L = l + r * 2 // 滑块实际边长

    	  function getRandomNumberByRange(start, end) {
    	    return Math.round(Math.random() * (end - start) + start)
    	  }

    	  function createCanvas(width, height) {
    	    const canvas = createElement('canvas')
    	    canvas.width = width
    	    canvas.height = height
    	    return canvas
    	  }

    	  function createImg(onload) {
    	    const img = createElement('img')
    	    img.crossOrigin = "Anonymous"
    	    img.onload = onload
    	    img.onerror = () => {
    	      img.src = getRandomImg()
    	    }
    	    img.src = getRandomImg()
    	    return img
    	  }
    	  
    	  function createElement(tagName) {
    	    return document.createElement(tagName)
    	  }

    	  function addClass(tag, className) {
    	    tag.classList.add(className)
    	  }

    	  function removeClass(tag, className) {
    	    tag.classList.remove(className)
    	  }
    	  
    	  function getRandomImg() {
    	    return 'https://picsum.photos/300/150/?image=' + getRandomNumberByRange(0, 100)
    	  }

    	  function draw(ctx, operation, x, y) {
    	    ctx.beginPath()
    	    ctx.moveTo(x, y)
    	    ctx.lineTo(x + l / 2, y)
    	    ctx.arc(x + l / 2, y - r + 2, r, 0, 2 * PI)
    	    ctx.lineTo(x + l / 2, y)
    	    ctx.lineTo(x + l, y)
    	    ctx.lineTo(x + l, y + l / 2)
    	    ctx.arc(x + l + r - 2, y + l / 2, r, 0, 2 * PI)
    	    ctx.lineTo(x + l, y + l / 2)
    	    ctx.lineTo(x + l, y + l)
    	    ctx.lineTo(x, y + l)
    	    ctx.lineTo(x, y)
    	    ctx.fillStyle = '#fff'
    	    ctx[operation]()
    	    ctx.beginPath()
    	    ctx.arc(x, y + l / 2, r, 1.5 * PI, 0.5 * PI)
    	    ctx.globalCompositeOperation = "xor"
    	    ctx.fill()
    	  }

    	  function sum(x, y) {
    	    return x + y
    	  }

    	  function square(x) {
    	    return x * x
    	  }

    	  class jigsaw {
    	    constructor(el, success, fail) {
    	      this.el = el
    	      this.success = success
    	      this.fail = fail
    	    }

    	    init() {
    	      this.initDOM()
    	      this.initImg()
    	      this.draw()
    	      this.bindEvents()
    	    }

    	    initDOM() {
    	      const canvas = createCanvas(w, h) // 画布
    	      const block = canvas.cloneNode(true) // 滑块
    	      const sliderContainer = createElement('div')
    	      const refreshIcon = createElement('div')
    	      const sliderMask = createElement('div')
    	      const slider = createElement('div')
    	      const sliderIcon = createElement('span')
    	      const text = createElement('span')

    	      block.className = 'block'
    	      sliderContainer.className = 'sliderContainer'
    	      refreshIcon.className = 'refreshIcon'
    	      sliderMask.className = 'sliderMask'
    	      slider.className = 'slider'
    	      sliderIcon.className = 'sliderIcon'
    	      text.innerHTML = '向右滑动滑块填充拼图'
    	      text.className = 'sliderText'

    	      const el = this.el
    	      el.appendChild(canvas)
    	      el.appendChild(refreshIcon)
    	      el.appendChild(block)
    	      slider.appendChild(sliderIcon)
    	      sliderMask.appendChild(slider)
    	      sliderContainer.appendChild(sliderMask)
    	      sliderContainer.appendChild(text)
    	      el.appendChild(sliderContainer)

    	      Object.assign(this, {
    	        canvas,
    	        block,
    	        sliderContainer,
    	        refreshIcon,
    	        slider,
    	        sliderMask,
    	        sliderIcon,
    	        text,
    	        canvasCtx: canvas.getContext('2d'),
    	        blockCtx: block.getContext('2d')
    	      })
    	    }

    	    initImg() {
    	      const img = createImg(() => {
    	        this.canvasCtx.drawImage(img, 0, 0, w, h)
    	        this.blockCtx.drawImage(img, 0, 0, w, h)
    	        const y = this.y - r * 2 + 2
    	        const ImageData = this.blockCtx.getImageData(this.x, y, L, L)
    	        this.block.width = L
    	        this.blockCtx.putImageData(ImageData, 0, y)
    	      })
    	      this.img = img
    	    }

    	    draw() {
    	      // 随机创建滑块的位置
    	      this.x = getRandomNumberByRange(L + 10, w - (L + 10))
    	      this.y = getRandomNumberByRange(10 + r * 2, h - (L + 10))
    	      draw(this.canvasCtx, 'fill', this.x, this.y)
    	      draw(this.blockCtx, 'clip', this.x, this.y)
    	    }

    	    clean() {
    	      this.canvasCtx.clearRect(0, 0, w, h)
    	      this.blockCtx.clearRect(0, 0, w, h)
    	      this.block.width = w
    	    }

    	    bindEvents() {
    	      this.el.onselectstart = () => false
    	      this.refreshIcon.onclick = () => {
    	        this.reset()
    	      }

    	      let originX, originY, trail = [], isMouseDown = false
    	      this.slider.addEventListener('mousedown', function (e) {
    	        originX = e.x, originY = e.y
    	        isMouseDown = true
    	      })
    	      document.addEventListener('mousemove', (e) => {
    	        if (!isMouseDown) return false
    	        const moveX = e.x - originX
    	        const moveY = e.y - originY
    	        if (moveX < 0 || moveX + 38 >= w) return false
    	        this.slider.style.left = moveX + 'px'
    	        var blockLeft = (w - 40 - 20) / (w - 40) * moveX
    	        this.block.style.left = blockLeft + 'px'

    	        addClass(this.sliderContainer, 'sliderContainer_active')
    	        this.sliderMask.style.width = moveX + 'px'
    	        trail.push(moveY)
    	      })
    	      document.addEventListener('mouseup', (e) => {
    	        if (!isMouseDown) return false
    	        isMouseDown = false
    	        if (e.x == originX) return false
    	        removeClass(this.sliderContainer, 'sliderContainer_active')
    	        this.trail = trail
    	        const {spliced, TuringTest} = this.verify()
    	        if (spliced) {
    	          if (TuringTest) {
    	            addClass(this.sliderContainer, 'sliderContainer_success')
    	            this.success && this.success()
    	          } else {
    	            addClass(this.sliderContainer, 'sliderContainer_fail')
    	            this.text.innerHTML = '再试一次'
    	            this.reset()
    	          }
    	        } else {
    	          addClass(this.sliderContainer, 'sliderContainer_fail')
    	          this.fail && this.fail()
    	          setTimeout(() => {
    	            this.reset()
    	          }, 1000)
    	        }
    	      })
    	    }

    	    verify() {
    	      const arr = this.trail // 拖动时y轴的移动距离
    	      const average = arr.reduce(sum) / arr.length // 平均值
    	      const deviations = arr.map(x => x - average) // 偏差数组
    	      const stddev = Math.sqrt(deviations.map(square).reduce(sum) / arr.length) // 标准差
    	      const left = parseInt(this.block.style.left)
    	      return {
    	        spliced: Math.abs(left - this.x) < 10,
    	        TuringTest: average !== stddev, // 只是简单的验证拖动轨迹，相等时一般为0，表示可能非人为操作
    	      }
    	    }

    	    reset() {
    	      this.sliderContainer.className = 'sliderContainer'
    	      this.slider.style.left = 0
    	      this.block.style.left = 0
    	      this.sliderMask.style.width = 0
    	      this.clean()
    	      this.img.src = getRandomImg()
    	      this.draw()
    	    }

    	  }

    	  window.jigsaw = {
    	    init: function (element, success, fail) {
    	      new jigsaw(element, success, fail).init()
    	    }
    	  }
    	}(window))
    
    </script>
    
   
  </body>
</html>